package com.exthirteen.textgamescript.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exthirteen.textgamescript.config.CommonConfig;
import com.exthirteen.textgamescript.entity.AuthConfig;
import com.exthirteen.textgamescript.mapper.AuthConfigMapper;
import com.exthirteen.textgamescript.service.AuthConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 配置服务
 * </p>
 *
 * @author exthirteen
 * @since 2025/5/4
 */
@Service
public class AuthConfigServiceImpl extends ServiceImpl<AuthConfigMapper, AuthConfig> implements AuthConfigService {
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public void setCallback(String callbackUrl) {
        AuthConfig authConfig = getConfig();
        authConfig.setCallbackUrl(callbackUrl);
        saveOrUpdate(authConfig);
        //设置socket回调地址
        CommonConfig.CALLBACK_URL = callbackUrl;
    }

    @Override
    public AuthConfig getConfig() {
        AuthConfig authConfig = getOne(new LambdaQueryWrapper<AuthConfig>()
                .last("limit 1"));
        if (authConfig == null) {
            authConfig = new AuthConfig();
            authConfig.setCallbackUrl("");
            save(authConfig);
        }
        return authConfig;
    }
}
